from flask import jsonify, Blueprint
from flask_restful import Api, Resource, reqparse

from models.model import *

vip_bp = Blueprint('vip_bp', __name__)
api = Api(vip_bp)


class VipView(Resource):
    def get(self):
        req = reqparse.RequestParser()
        req.add_argument('name')
        req.add_argument('cardNum')
        req.add_argument('birthday')
        req.add_argument('payType', type=int)
        req.add_argument('page', type=int)
        req.add_argument('per_page', type=int)
        args = req.parse_args()
        name = args['name']
        cardNum = args['cardNum']
        birthday = args['birthday']
        payType = args['payType']
        page = args['page']
        limit = args['per_page']
        sql = Vip.query
        if name:
            sql = sql.filter(Vip.name.like(f"%{name}%"))
        if cardNum:
            sql = sql.filter(Vip.cardNum.like(f"%{cardNum}%"))
        if birthday:
            sql = sql.filter(Vip.birthday.like(f"%{birthday}%"))
        if payType:
            sql = sql.filter(Vip.payType.like(f"%{payType}%"))
        if not page:
            page = 1
        if not limit:
            limit = 5
        vip_info = sql.paginate(page, limit)
        vip_list = []

        for i in vip_info.items:
            a = {
                'id': i.id,
                'name': i.name,
                'cardNum': i.cardNum,
                'integral': i.integral,
                'phone': i.phone,
                'money': i.money,
                'address': i.address,
                'payType': i.payType,
                'birthday': i.birthday
            }
            vip_list.append(a)
        return jsonify({
            'code': 200,
            'count': vip_info.total,
            'msg': '获取成功',
            'vendor_list': vip_list
        })

    def post(self):
        req = reqparse.RequestParser()
        req.add_argument('name', required=True)
        req.add_argument('cardNum', required=True)
        req.add_argument('integral', type=int, required=True)
        req.add_argument('phone', required=True)
        req.add_argument('money', required=True)
        req.add_argument('address', required=True)
        req.add_argument('payType', type=int, required=True)
        req.add_argument('birthday', required=True)
        args = req.parse_args()
        try:
            db.session.add(Vip(**args))
            db.session.commit()
        except Exception as e:
            print(e)
            return jsonify({
                'code': 400,
                'msg': '添加失败'
            })
        return jsonify({
            'code': 200,
            'msg': '添加成功'
        })


class VipOneView(Resource):
    def get(self, id):
        vip_one = Vip.query.filter(Vip.id == id).first()
        if not vip_one:
            return jsonify({
                'code': 400,
                'msg': '商品不存在'
            })
        vip_list = {
            'id': vip_one.id,
            'name': vip_one.name,
            'cardNum': vip_one.cardNum,
            'integral': vip_one.integral,
            'phone': vip_one.phone,
            'money': vip_one.money,
            'address': vip_one.address,
            'payType': vip_one.payType,
            'birthday': vip_one.birthday
        }
        return jsonify({
            'code': 200,
            'msg': "获取成功",
            'vendor_list': vip_list
        })

    def put(self, id):
        req = reqparse.RequestParser()
        req.add_argument('name', required=True)
        req.add_argument('cardNum', required=True)
        req.add_argument('integral', type=int, required=True)
        req.add_argument('phone', required=True)
        req.add_argument('money', required=True)
        req.add_argument('address', required=True)
        req.add_argument('payType', type=int, required=True)
        req.add_argument('birthday', required=True)
        args = req.parse_args()
        vip_one = Vip.query.filter(Vip.id == id).first()
        if not vip_one:
            return jsonify({
                'code': 400,
                'msg': '商品不存在'
            })
        try:
            Vip.query.filter(Vip.id == id).update(args)
            db.session.commit()
            return jsonify({
                'code': 200,
                'msg': '更新成功'
            })
        except:
            return jsonify({
                'code': 400,
                'msg': '更新失败'
            })

    def delete(self, id):
        vip_one = Vip.query.filter(Vip.id == id).first()
        if not vip_one:
            return jsonify({
                'code': 400,
                'msg': '商品不存在'
            })
        Vip.query.filter(Vip.id == id).delete()
        db.session.commit()
        return jsonify({
            'code': 200,
            'msg': '删除成功'
        })


api.add_resource(VipView, '/v2/member')
api.add_resource(VipOneView, '/v2/member/<int:id>')
